﻿
package com.android.finaltracer.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.android.finaltracer.model.FTDataProvider.RelationColumns;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;

public class MarkerOverlay extends ItemizedOverlay<OverlayItem> {

    public interface PopCallback {
        public void popChanged(GeoPoint pt, boolean visible, String snippet);
    }

    private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>(); // 坐标序列

    private Drawable marker;
    private Context mContext;
    private PopCallback mPopCallback;

    public MarkerOverlay(Context context, Drawable marker, PopCallback cb) {
        super(boundCenter(marker));

        this.marker = marker;
        this.mContext = context;
        this.mPopCallback = cb;
    }

    public void clear() {
        mGeoList.clear();
        this.populate();
    }

    public void restoreFromCursor(Cursor cursor) {
        Log.d("Overlay", "restoreFromCursor ");
        mGeoList.clear();

        if (cursor != null && cursor.moveToFirst()) {
            do {
                String number = cursor.getString(cursor
                        .getColumnIndexOrThrow(RelationColumns.NUMBER));
                int lat = cursor.getInt(cursor
                        .getColumnIndexOrThrow(RelationColumns.GPS_LAT));
                int lng = cursor.getInt(cursor
                        .getColumnIndexOrThrow(RelationColumns.GPS_LNG));
                Log.d("FTMapActivity", String.format(
                        "get [%s] position {%d, %d}", number, lat, lng));

                GeoPoint point = new GeoPoint(lat, lng);
                mGeoList.add(new OverlayItem(point, number, number
                        + " is here!"));

            } while (cursor.moveToNext());
        }

        this.populate();
    }

    // 构造OverlayItem的三个参数依次为：位置，标题，文字片段
    public void addItem(GeoPoint point, String title, String snippet) {
        mGeoList.add(new OverlayItem(point, title, snippet));
        this.populate();
    }

    public void removeItem(int index) {
        mGeoList.remove(index);
        this.populate();
    }

    public GeoPoint getFirstPoint() {
        if (mGeoList.size() > 0) {
            return mGeoList.get(0).getPoint();
        }

        return null;
    }

    @Override
    protected OverlayItem createItem(int index) {
        Log.d("Overlay", "createItem " + index + ", " + mGeoList.get(index));
        // TODO Auto-generated method stub
        return mGeoList.get(index);
    }

    @Override
    public int size() {
        Log.d("Overlay", "size(): " + mGeoList.size());
        // TODO Auto-generated method stub
        return mGeoList.size();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        if (size() > 0) {
            super.draw(canvas, mapView, shadow);
            // 调整一个drawable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
            boundCenter(marker);
        }
    }

    @Override
    // 处理当点击事件
    protected boolean onTap(int i) {
        setFocus(mGeoList.get(i));
        // 更新气泡位置,并使之显示
        GeoPoint pt = mGeoList.get(i).getPoint();
        if (mPopCallback != null) {
            mPopCallback.popChanged(pt, true, mGeoList.get(i).getSnippet());
        }
        return true;
    }

    @Override
    public boolean onTap(GeoPoint arg0, MapView arg1) {
        // TODO Auto-generated method stub
        // 消去弹出的气泡
        if (mPopCallback != null) {
            mPopCallback.popChanged(arg0, false, null);
        }
        return true;// super.onTap(arg0, arg1);
    }
}
